package com.zycfc.zsf.boot.exception.error;

import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.boot.web.servlet.error.*;
import org.springframework.beans.factory.annotation.*;
import com.zycfc.zsf.boot.exception.handler.*;
import javax.servlet.http.*;
import com.zycfc.zsf.boot.exception.*;
import org.springframework.util.*;
import java.util.*;
import org.springframework.web.context.request.*;
import org.slf4j.*;

@Controller
@RequestMapping({ "${server.error.path:${error.path:/error}}" })
public class ErrorEndpoint implements ErrorController
{
    private static final Logger log;
    @Autowired
    private ErrorAttributes errorAttributes;
    @Value("${server.error.path:${error.path:/error}}")
    private String path;
    @Autowired
    private ZsfExceptionHandler handler;
    
    @RequestMapping
    public void error(final HttpServletRequest request, final HttpServletResponse response) {
        try {
            final ExceptionInfo res = this.buildBody(request, false);
            this.handler.sendErrorMsg(request, response, res);
        }
        catch (Throwable e) {
            ErrorEndpoint.log.error(e.getMessage(), e);
        }
    }
    
    private ExceptionInfo buildBody(final HttpServletRequest request, final Boolean includeStackTrace) {
        final Map<String, Object> errorAttributes_ = this.getErrorAttributes(request, includeStackTrace);
        final Integer status = errorAttributes_.get("status");
        String message = errorAttributes_.get("message");
        if (includeStackTrace) {
            final String trace = errorAttributes_.get("trace");
            if (!StringUtils.isEmpty((Object)trace)) {
                message += String.format(" and trace %s", trace);
            }
        }
        return new ExceptionInfo(String.valueOf(status), message);
    }
    
    public String getErrorPath() {
        return this.path;
    }
    
    private Map<String, Object> getErrorAttributes(final HttpServletRequest request, final boolean includeStackTrace) {
        final RequestAttributes requestAttributes = (RequestAttributes)new ServletRequestAttributes(request);
        return new HashMap<String, Object>();
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ErrorEndpoint.class);
    }
}
